<template>
  <el-dialog
    v-model="dialogVisible"
    title="平台海报模板"
    width="820"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <div class="template-wrap">
      <div
        class="tempalte-item mb20 mr9"
        :class="{ active: selectedImgsIDs.includes(item.advertisingId) }"
        v-for="(item, index) in TemplateList"
        :key="index"
        @click="handImg(item)"
      >
        <div class="img-wrap">
          <img :src="item.img" alt="" />
        </div>

        <div class="select-icon">
          <el-icon v-if="selectedImgsIDs.includes(item.advertisingId)"><CircleCheckFilled /></el-icon>
        </div>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button :disabled="!selectedImgs.length > 0" type="primary" @click="submit">确认上传</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from "vue";
import useMultipartOssStore from "@/store/modules/multipartOssStore.js";
const ossStore = useMultipartOssStore();
import { ElMessage } from "element-plus";
import { _debounce } from "@/utils/index"; //防抖，多次频繁触发时，只执行最后一次
import { CloseBold, Loading, CircleCheckFilled } from "@element-plus/icons-vue";
import { getFolder } from "@/utils/index";

const emit = defineEmits(["submit", "close"]);
const props = defineProps({
  optionInfo: {
    type: Object,
    default: () => {
      return {
        isShow: false,
        templateList: []
      };
    }
  }
});
const dialogVisible = ref(false);
const FileList = ref([]);
const uploadImg = ref();
const TemplateList = ref([]);
const selectedImgs = ref([]);
const selectedImgsIDs = ref([]);

watch(
  () => props.optionInfo,
  val => {
    dialogVisible.value = val.isShow;
    TemplateList.value = val.templateList;
  },
  { deep: true }
);

const handImg = item => {
  if (selectedImgsIDs.value.includes(item.advertisingId)) {
    const index = selectedImgs.value.findIndex(_item => {
      _item.advertisingId === item.advertisingId;
    });
    const _index = selectedImgsIDs.value.findIndex(_item2 => {
      _item2 === item.advertisingId;
    });
    selectedImgs.value.splice(index, 1);
    selectedImgsIDs.value.splice(_index, 1);
    console.log(111, selectedImgsIDs.value, item.advertisingId, selectedImgsIDs.value.includes(item.advertisingId));
  } else {
    console.log(222);
    selectedImgs.value.push(item);
    selectedImgsIDs.value.push(item.advertisingId);
  }
  console.log("选中的图片===", selectedImgs.value);
};

const closeDialog = () => {
  //   resetForm(); //清空表单
  selectedImgs.value = [];
  selectedImgsIDs.value = [];
  emit("close");
};
const submit = async () => {
  emit("submit", selectedImgs.value, false);
  selectedImgs.value = [];
};
</script>

<style lang="scss" scoped>
p {
  margin: 0;
}
:deep(.el-upload-dragger) {
  background-color: #f2f3f5;
}
.el-icon--upload {
  height: auto;
  margin-bottom: 10px;
}
.img-wrap {
  width: 180px;
  height: 120px;
  border-radius: 10px;
}
.template-wrap {
  display: flex;
  flex-wrap: wrap;
  .tempalte-item {
    width: 180px;
    height: 120px;
    border-radius: 10px;
    border: 2px solid transparent;
    position: relative;
    .select-icon {
      position: absolute;
      top: 10px;
      left: 10px;
      color: #007dff;
      width: 20px;
      height: 20px;
      font-size: 20px;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.5);
    }
    &.active {
      border: 2px solid #007dff;
    }
    .img-wrap {
      width: 100%;
      height: 100%;
      border-radius: 10px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
      }
    }
  }
}
</style>
